import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;


public class DDF2 {
	int xRows, xCols; 
	
	@Test
	public void myMainTest() throws Exception{
		System.out.println("Main Test");
		
		// Read each set of Test Data
		String xlPath = "C:/Selenium/A16_2012/YT_Playing Around.xls";
		String[][] myXL = readXL(xlPath, "Test Cases");
		System.out.println(" Somedata is " + myXL[1][2]);
		for (int i=0; i<xRows; i++) {
			for (int j=0; j<xCols; j++){
				System.out.println(myXL[i][j]);
			}
		}
	}
	
	// Method to read XL
	public String[][] readXL(String sPath, String iSheet) throws Exception{

		String[][] xData;                                                                
		
		File myxl = new File(sPath);                                
		FileInputStream myStream = new FileInputStream(myxl);                                
		HSSFWorkbook myWB = new HSSFWorkbook(myStream);                                
		HSSFSheet mySheet = myWB.getSheet(iSheet);                                 
		xRows = mySheet.getLastRowNum()+1;                                
		xCols = mySheet.getRow(0).getLastCellNum();                                
		xData = new String[xRows][xCols];        
		for (int i = 0; i < xRows; i++) {                           
				HSSFRow row = mySheet.getRow(i);
				for (int j = 0; j < xCols; j++) {                               
					HSSFCell cell = row.getCell(j);                               
					String value = cellToString(cell);                               
					xData[i][j] = value;                               
					}        
				}                                      
		return xData;
	}
	
	// Method to write into an XL

	
	//Change cell type
	public static String cellToString(HSSFCell cell) { 
		// This function will convert an object of type excel cell to a string value
		int type = cell.getCellType();                        
		Object result;                        
		switch (type) {                            
			case HSSFCell.CELL_TYPE_NUMERIC: //0                                
				result = cell.getNumericCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_STRING: //1                                
				result = cell.getStringCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_FORMULA: //2                                
				throw new RuntimeException("We can't evaluate formulas in Java");  
				case HSSFCell.CELL_TYPE_BLANK: //3                                
				result = "-";                                
				break;                            
			case HSSFCell.CELL_TYPE_BOOLEAN: //4     
				result = cell.getBooleanCellValue();       
				break;                            
			case HSSFCell.CELL_TYPE_ERROR: //5       
				throw new RuntimeException ("This cell has an error");    
			default:                  
				throw new RuntimeException("We don't support this cell type: " + type); 
				}                        
		return result.toString();      
		}
}
